package com.leoman.spinningmachine.login;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.leoman.helper.util.LogUtil;
import com.leoman.helper.util.ToastUtil;
import com.leoman.spinningmachine.R;
import com.leoman.spinningmachine.base.BaseActivity;
import com.leoman.spinningmachine.bean.User;
import com.leoman.spinningmachine.constant.Constant;
import com.leoman.spinningmachine.http.APIResponse;
import com.leoman.spinningmachine.http.WebServiceApi;
import com.leoman.spinningmachine.main.MainActivity;
import com.leoman.spinningmachine.util.DialogUtils;
import com.leoman.spinningmachine.util.MacUtil;
import com.leoman.spinningmachine.util.PhoneMac;
import com.leoman.spinningmachine.util.SharedPreferencesUtils;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;

/**
 * 登录
 */
public class LoginActivity extends BaseActivity {

    @BindView(R.id.et_account)
    EditText etAccount;
    @BindView(R.id.iv_pwd)
    ImageView ivPwd;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.tv_login)
    TextView tvLogin;

    private boolean isShowPassWord;
    private String registrationID;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.ac_login;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        if (SharedPreferencesUtils.getInstance().getBoolean(Constant.ISLOGIN)) {
            LogUtil.json("token:" + SharedPreferencesUtils.getInstance().getString("token"));
            gotoOtherActivity(MainActivity.class);
            finish();
        }
        setBarColor(false, R.color.trans);
        setFullscreen(true);

        etAccount.setText(SharedPreferencesUtils.getInstance().getString(Constant.NUMBER));
        etPwd.setText(SharedPreferencesUtils.getInstance().getString(Constant.PASSWORD));

    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.iv_pwd, R.id.tv_login, R.id.tv_register})
    void click(View view) {
        switch (view.getId()) {
            case R.id.iv_pwd:
                String password = etPwd.getText().toString().trim();
                isShowPassWord = !isShowPassWord;
                ivPwd.setImageResource(isShowPassWord ? R.drawable.ic_eye_open : R.drawable.ic_eye_close);
                etPwd.setInputType(isShowPassWord ? InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD : InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                if (!TextUtils.isEmpty(password)) {
                    etPwd.setSelection(etPwd.getText().length());
                }
                break;
            case R.id.tv_login:
                commit();
                break;
            case R.id.tv_register:
                String phoneMac = PhoneMac.getLocalMacAddressFromIp();
                if (TextUtils.isEmpty(phoneMac)) {
                    ToastUtil.showToast(this, "当前网络为非WiFi网络，请连接WiFi后注册", Toast.LENGTH_SHORT);
                } else {
                    startActivityForResult(new Intent(this, RegisterActivity.class), 101);
                }
                break;
        }
    }

    private String mobile, password;

    private void commit() {
        mobile = etAccount.getText().toString().trim();
        password = etPwd.getText().toString().trim();

        if (TextUtils.isEmpty(mobile)) {
            DialogUtils.getInstance().showResultDialog(this, "请输入您的工号或手机号");
        } else if (TextUtils.isEmpty(password)) {
            DialogUtils.getInstance().showResultDialog(this, "请输入您的密码");
        } else {

            String mac = MacUtil.getMacAddress(this);
            DialogUtils.getInstance().show(this);
            LogUtil.json("mac：" + PhoneMac.getLocalMacAddressFromIp());
            registrationID = JPushInterface.getRegistrationID(this);
            WebServiceApi.getInstance().login(mobile, password, mac, registrationID, this, 1);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK && requestCode == 101) {
            mobile = data.getStringExtra("data");
            if (!TextUtils.isEmpty(mobile)) {
                etAccount.setText(mobile);
                etAccount.setSelection(mobile.length());
            }
        }
    }

    @Override
    public void OnSuccessData(APIResponse apiResponse, Integer tag) {
        tvLogin.setEnabled(true);
        if (1 == tag) {

            SharedPreferencesUtils.getInstance().putString(Constant.NUMBER, mobile);
            SharedPreferencesUtils.getInstance().putString(Constant.PASSWORD, password);

            User bean = apiResponse.data.user;
            if (bean != null) {
                SharedPreferencesUtils.getInstance().putString(Constant.USERID, "" + bean.getId());
                SharedPreferencesUtils.getInstance().putString("token", bean.getToken());
                SharedPreferencesUtils.getInstance().putString(Constant.DEPYNAME, bean.getDeptName());
                SharedPreferencesUtils.getInstance().putBoolean(Constant.ISLOGIN, true);
                SharedPreferencesUtils.getInstance().putBoolean(Constant.ISLOGIN, true);
                SharedPreferencesUtils.getInstance().putString(Constant.NICKNAME, bean.getNickname());
                SharedPreferencesUtils.getInstance().putString(Constant.PHONE, bean.getPhone());
                SharedPreferencesUtils.getInstance().putInt(Constant.USERGH, bean.getWorkId());
                SharedPreferencesUtils.getInstance().putString(Constant.HEADPATH, bean.getHeadImage());
                SharedPreferencesUtils.getInstance().putInt(Constant.CAMERAQ, bean.getCameraq());

                if (bean.getManager() != null) {
                    SharedPreferencesUtils.getInstance().putInt(Constant.USER_MANAGERID, bean.getManager().getId());
                    SharedPreferencesUtils.getInstance().putString(Constant.USER_MANAGER, bean.getManager().getNickname());
                    SharedPreferencesUtils.getInstance().putString(Constant.USER_MANAGERIMAGE, bean.getManager().getHeadImage());
                    SharedPreferencesUtils.getInstance().putString(Constant.USER_REALNAME, bean.getManager().getRealName());
                }
            }

            DialogUtils.getInstance().cancel();
            LogUtil.json("token", bean.getToken());
            gotoOtherActivity(MainActivity.class);
            finish();
        }
    }

    @Override
    public void OnErrorData(String code, Integer tag) {
        DialogUtils.getInstance().cancel();
        DialogUtils.getInstance().showResultDialog(this, code);
    }
}
